'use strict'
const path = require('path')

const nconf = require('nconf').file({
  file: process.platform === 'win32' ? `${process.env['USERPROFILE']}\\_ew.json` : `${process.env['HOME']}/.ew.json`
})

function isOK() {
  const check = (key, val) => { if(typeof(nconf.get(key)) === "undefined") nconf.set(key, val) }
  try {
    nconf.load()
    check("language", "en")
    check("dev-mode", false)
    check("multi-instance", true)
    check("tray-color", "white")
    check("prevent-recall", true)
    check("window-size:width", 880)
    check("window-size:height", 660)
    check("download-dir", process.platform === 'win32' ? path.join("_cache", "electronjs-wechat", "download") : path.join(".cache", "electronjs-wechat", "download"))
    check("logger-level", "OFF")
    nconf.save()
  } catch(error) {
    console.log(error)
    return false
  }
  return true
}

function saveSettings(settingKey, settingValue) {
  nconf.set(settingKey, settingValue)
  nconf.save()
}

function readSettings(settingKey) {
  nconf.load()
  return nconf.get(settingKey)
}

module.exports = {
  saveSettings,
  readSettings,
  isOK
}
